package cn.larry.config;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.support.ConversionServiceFactoryBean;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.validation.DataBinder;

/**
 * @author LarryZeal
 *
 */
@Profile("conversionService")
@Configuration
public class ConversionServiceConfig {

	/*
	 * @Autowired
	 * public ConversionService conversionService; // spring boot 默认木有这个，所以无法注入啊。
	 * 
	 * @PostConstruct
	 * public void run(){
	 * Integer convert = conversionService.convert("123", int.class);
	 * System.out.println(convert);
	 * }
	 */

	// GenericConversionService 没有注册converters，需要手动注册。
/*	@Bean
	public ConversionService conversionService() {
		GenericConversionService conversionService = new GenericConversionService();
		boolean canConvert = conversionService.canConvert(String.class, int.class);
		System.out.println("------------"+canConvert+"----------"); //GenericConversionService默认没有convert
		Integer convert = conversionService.convert("123", int.class);
		System.out.println(convert);
		return conversionService;
	}*/
	
	// DefaultConversionService 才默认注册了converters。
/*	@Bean
	public ConversionService conversionService1() {
		DefaultConversionService conversionService = new DefaultConversionService();
		boolean canConvert = conversionService.canConvert(String.class, int.class);
		System.out.println("------------"+canConvert+"----------");
		Integer convert = conversionService.convert("123", int.class);
		System.out.println(convert);
		return conversionService;
	}*/
	
	/**
	 * This implementation creates a DefaultConversionService. 
	 * Subclasses may override createConversionService() in order to return a GenericConversionService instance of their choosing. 
	 * 
	 * 这个可以取代上面的DefaultConversionService，基本一致。
	 * 
	 * @return
	 */
	@Bean
	public ConversionServiceFactoryBean conversionService(){
		return new ConversionServiceFactoryBean();
	}
	
	private DataBinder dataBinder;
	
}
